home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_527 / toolmanager / source / toolmanager.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  10KB  |  350 lines

  1. /*
  2.  * toolmanager.c   V1.4
  3.  *
  4.  * startup routines
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Library pointers */
  12. extern struct Library *SysBase,*IntuitionBase,*GfxBase; /* Autoinit Libs */
  13. extern struct Library *GadToolsBase;                    /* Autoinit Libs */
  14. struct Library *WorkbenchBase,*IconBase,*CxBase;
  15.  
  16. /* Tooltypes "YES" string */
  17. static char YesString[]="YES";
  18.  
  19. /* ReadArgs() stuff */
  20. static char Template[]=
  21.   "NOICON/S,CONFIG/K,POPUP/S,POPKEY/K,CXPRIO/K/N,NOREQ/S,Tools/M";
  22. static long defcxprio=0;
  23. static struct { /* All entries in this array MUST be longwords! */
  24.                long noicon;
  25.                char *config;
  26.                long popup;
  27.                char *popkey;
  28.                long *cxprio;
  29.                long noreq;
  30.                char **tools;
  31.               } def={FALSE,NULL,FALSE,NULL,&defcxprio,FALSE,NULL};
  32.  
  33. /* miscellaneous */
  34. extern char InternalConfigName[];
  35. extern struct NewBroker nb;
  36. static struct DiskObject *dobj=NULL;
  37. static struct AppMenuItem *QTMAppMenuItem;
  38. static APTR OldConsoleTask=NULL;
  39.  
  40. /* Open libraries and other stuff */
  41. static BOOL openstuff(void)
  42. {
  43.  if (SysBase->lib_Version<36)  /* Sanity check */
  44.   {
  45.    puts("You need a Kickstart 2.0 or better to run " DEFPROGNAME "!");
  46.    exit(20);
  47.   }
  48.  
  49.  /* Open libraries */
  50.  if (!(WorkbenchBase=OpenLibrary(WORKBENCH_NAME,36)))
  51.   cleanup(1);
  52.  
  53.  if (!(IconBase=OpenLibrary(ICONNAME,0)))
  54.   cleanup(2);
  55.  
  56.  if (!(CxBase=OpenLibrary("commodities.library",36)))
  57.   cleanup(3);
  58.  
  59.  Forbid();                     /* Is "ToolManager" already running? */
  60.  MyMP=FindPort(MyName);
  61.  Permit();
  62.  if (MyMP) return(TRUE);       /* Yes, don't start again! */
  63.  
  64.  if (!(MyMP=CreateMsgPort()))  /* No, create message port */
  65.   cleanup(4);
  66.  MyMP->mp_Node.ln_Pri=0;
  67.  MyMP->mp_Node.ln_Name=MyName; /* Our name */
  68.  AddPort(MyMP);                /* Announce our presence */
  69.  
  70.  /* Create Commodities Broker port */
  71.  if (!(MyBrokerPort=CreateMsgPort()))
  72.   cleanup(5);
  73.  MyBrokerPort->mp_Node.ln_Pri=0;
  74.  MyBrokerPort->mp_Node.ln_Name="TM Broker Port";
  75.  nb.nb_Port=MyBrokerPort;
  76.  
  77.  /* Create Broker */
  78.  if (!(MyBroker=CxBroker(&nb,NULL)))
  79.   cleanup(6);
  80.  return(FALSE);                /* No, start ToolManager */
  81. }
  82.  
  83. /* Init tool stuff */
  84. static void inittool(void)
  85. {
  86.  SetConfigFileName(InternalConfigName); /* Copy config file name */
  87.  NewList(&ToolList);                    /* Initialize tool list */
  88.  
  89.  /* Notify Workbench about special menu items. */
  90.  /* 1. If this item is selected, the program will quit */
  91.  if (!(QTMAppMenuItem=AddAppMenuItem(0,NULL,"Quit ToolManager",MyMP,TAG_DONE)))
  92.   cleanup(7);
  93.  
  94.  /* 2. If this item is selected, the status window will open */
  95.  if (!(OTWAppMenuItem=AddAppMenuItem(1,NULL,"Open TM Window",MyMP,TAG_DONE)))
  96.   cleanup(8);
  97. }
  98.  
  99. /* Send an AppMessage with our parameters to a running ToolManager process */
  100. static void SendParameters(LONG nargs, struct WBArg *arg)
  101. {
  102.  struct MsgPort *sp,*rp;
  103.  struct AppMessage *msg;
  104.  
  105.  /* Allocate memory for AppMessage */
  106.  if (!(msg=malloc(sizeof(struct AppMessage)))) goto e1;
  107.  
  108.  /* Create a reply port */
  109.  if (!(rp=CreateMsgPort())) goto e2;
  110.  
  111.  /* Build AppMessage */
  112.  msg->am_Message.mn_Node.ln_Type=NT_MESSAGE;
  113.  msg->am_Message.mn_Node.ln_Pri=0;
  114.  msg->am_Message.mn_ReplyPort=rp;
  115.  msg->am_Type=MTYPE_APPICON;
  116.  msg->am_NumArgs=nargs;
  117.  msg->am_ArgList=arg;
  118.  
  119.  Forbid();                     /* Find "ToolManager" message port */
  120.  sp=FindPort(MyName);
  121.  if (sp) PutMsg(sp,(struct Message *) msg); /* Send AppMessage */
  122.  Permit();
  123.  
  124.  if (sp)
  125.   {                            /* We have send the message */
  126.    WaitPort(rp);               /* Wait on the reply */
  127.    GetMsg(rp);                 /* Remove reply from port */
  128.   }
  129.  
  130.     DeleteMsgPort(rp);
  131. e2: free(msg);
  132. e1: return;
  133. }
  134.  
  135. /* Workbench main entry point */
  136. void wbmain(struct WBStartup *wbarg)
  137. {
  138.  BPTR fl;
  139.  
  140.  if (openstuff())         /* common startup code */
  141.   {                       /* ToolManager already running, send parameters */
  142.    if (wbarg->sm_NumArgs>1)
  143.     SendParameters(wbarg->sm_NumArgs-1,wbarg->sm_ArgList+1);
  144.    cleanup(4);            /* All done */
  145.   }
  146.  
  147.  inittool();              /* Init tool stuff */
  148.  
  149.  /* Process WB startup parameters */
  150.  WBAddToolNode(wbarg->sm_ArgList+1,wbarg->sm_NumArgs-1);
  151.  
  152.  /* Set global startup current directory */
  153.  StartupCD=wbarg->sm_ArgList->wa_Lock;
  154.  
  155.  /* Get the program icon */
  156.  fl=CurrentDir(StartupCD);
  157.  if (dobj=GetDiskObject(wbarg->sm_ArgList->wa_Name))
  158.   {
  159.    char *cp1,*cp2,**ttp=dobj->do_ToolTypes;
  160.  
  161.    /* Retreive ToolTypes from icon */
  162.    /* 1. CONFIG=<name> Set configuration file name */
  163.    if (cp1=FindToolType(ttp,"CONFIG")) SetConfigFileName(cp1);
  164.  
  165.    /* 2. CX_POPKEY=<key> Set commodities HotKey */
  166.    if (cp1=FindToolType(ttp,"CX_POPKEY")) PopUpHotKey=strdup(cp1);
  167.    if (!PopUpHotKey) PopUpHotKey=DefaultPopUpHotKey;
  168.  
  169.    /* 3. CX_POPUP=YES|NO Show status window on startup */
  170.    if (cp1=FindToolType(ttp,"CX_POPUP"))
  171.     ShowStatusWindow=MatchToolValue(cp1,YesString);
  172.  
  173.    /* 4. CX_PRIORITY=<num> Set broker priority */
  174.    if (cp1=FindToolType(ttp,"CX_PRIORITY")) nb.nb_Pri=strtol(cp1,&cp2,10);
  175.  
  176.    /* 5. SHOWREQ=YES|NO Show requester */
  177.    if (cp1=FindToolType(ttp,"SHOWREQ"))
  178.     ShowQuitReq=MatchToolValue(cp1,YesString);
  179.  
  180.    /* 6. INTERNALICON Use the internal icon as AppIcon */
  181.    if (!FindToolType(ttp,"INTERNALICON")) MyIcon=dobj;
  182.  
  183.    /* 7. SHOWICON=YES|NO Show AppIcon */
  184.    if (cp1=FindToolType(ttp,"SHOWICON"))
  185.     if (!(ShowIcon=MatchToolValue(cp1,YesString)))
  186.      {
  187.       FreeDiskObject(dobj);
  188.       dobj=NULL;
  189.      }
  190.   }
  191.  CurrentDir(fl);
  192.  
  193.  /* Read configuration file */
  194.  ReadConfigFile();
  195.  
  196.  mainloop(); /* Go into main loop */
  197. }
  198.  
  199. /* CLI main entry point */
  200. void main(int argc, char **argv)
  201. {
  202.  struct RDArgs *rda;
  203.  struct Process *pr=FindTask(NULL);
  204.  
  205.  /* Help requested? */
  206.  if ((argc>1) && (*argv[1]=='?'))
  207.   { /* Print out usage, but DON'T start ToolManager */
  208.    puts("");
  209.    puts(CopyrightNote);
  210.    fputs("\nUsage: ToolManager ",stdout);
  211.    puts(Template);
  212.    puts("\n Parameter       Description                         Default");
  213.    puts(" NOICON        : Don't show the program icon         Show icon");
  214.    puts(" CONFIG <file> : Name of configuration file          " DEFCONFIGNAME);
  215.    puts(" POPUP         : Open status window after startup    Don't open");
  216.    puts(" POPKEY <key>  : Commodities HotKey definition       \"" DEFPOPUPHOTKEY "\"");
  217.    puts(" CXPRIO <num>  : Commodities priority                0");
  218.    puts(" NOREQ         : Don't display quit requester        Show requester");
  219.    puts(" Tools         : Name of programs to add to the menu");
  220.    puts("\nRequires Kickstart 2.0 or better!");
  221.    exit(0);
  222.   }
  223.  
  224.  freopen("*","w",stderr);     /* Reopen the stderr for banner line */
  225.  fputs(CopyrightNote,stderr); /* Put out banner line (only in CLI) */
  226.  fputc('\n',stderr);
  227.  fclose(stderr);
  228.  
  229.  /* Set global startup current directory */
  230.  StartupCD=CurrentDir(NULL);
  231.  CurrentDir(StartupCD);
  232.  
  233.  if (openstuff())             /* common startup code */
  234.   {                           /* ToolManager already running, send parameters */
  235.    struct WBArg *wa;
  236.  
  237.    if (argc<2) cleanup(4);    /* No parameters to send */
  238.  
  239.    /* Allocate memory for WB parameters */
  240.    if (!(wa=malloc(sizeof(struct WBArg)*(argc-1)))) cleanup(4);
  241.  
  242.    /* Process CLI startup parameters */
  243.    if (rda=ReadArgs(Template,(LONG *) &def,NULL))
  244.     {
  245.      /* Build WBArgs */
  246.      if (argv=def.tools)
  247.       {
  248.        register struct WBArg *wat=wa;
  249.  
  250.        argc=0;
  251.        while (*argv)              /* Scan list of tools */
  252.         {
  253.          wat->wa_Lock=StartupCD;  /* Copy parameters */
  254.          wat->wa_Name=*argv++;
  255.          wat++;
  256.          argc++;
  257.         }
  258.  
  259.        if (argc>0) SendParameters(argc,wa); /* Send parameters */
  260.       }
  261.      FreeArgs(rda);           /* Free RDArgs */
  262.     }
  263.  
  264.    free(wa);                  /* Free WB parameters */
  265.    cleanup(4);                /* All done */
  266.   }
  267.  
  268.  inittool();                  /* Init tool stuff */
  269.  
  270.  /* Process CLI startup parameters */
  271.  rda=ReadArgs(Template,(LONG *) &def,NULL);
  272.  
  273.  /* Set internal values according to command line parameters */
  274.  ShowIcon=!def.noicon;
  275.  if (def.config) SetConfigFileName(def.config);
  276.  ShowStatusWindow=def.popup;
  277.  if (def.popkey) PopUpHotKey=strdup(def.popkey);
  278.  if (!PopUpHotKey) PopUpHotKey=DefaultPopUpHotKey;
  279.  nb.nb_Pri=*def.cxprio;
  280.  ShowQuitReq=!def.noreq;
  281.  
  282.  /* Add every other parameter as CLI tool */
  283.  if (argv=def.tools)
  284.   {
  285.    struct ConfigBlock *cb;
  286.  
  287.    if (cb=malloc(sizeof(struct ConfigBlock))) /* Get memory */
  288.     {
  289.      /* Init config block */
  290.      InitConfigBlock(cb);
  291.      cb->cb_Type=TNTYPE_CLI;
  292.  
  293.      while (*argv)
  294.       {
  295.        strncpy(cb->cb_Alias,*argv++,BUFLEN-1);
  296.        AddToolNode(cb,StartupCD);
  297.       }
  298.  
  299.      free(cb);
  300.     }
  301.   }
  302.  
  303.  /* Free RDArgs */
  304.  if (rda) FreeArgs(rda);
  305.  
  306.  ReadConfigFile();            /* Read configuration file */
  307.  
  308.  fclose(stdin);               /* Detach from console window */
  309.  fclose(stdout);
  310.  OldConsoleTask=pr->pr_ConsoleTask;
  311.  pr->pr_ConsoleTask=NULL;
  312.  mainloop();                  /* Go into main loop */
  313. }
  314.  
  315. /* Final cleanup routine */
  316. void cleanup(int i)
  317. {
  318.  register struct Message *msg;
  319.  
  320.  switch(i)
  321.   {
  322.    case 99:
  323.    case 10:if (ShowIcon) RemoveAppIcon(MyAppIcon);
  324.            if (dobj) FreeDiskObject(dobj);
  325.    case  9:RemoveTools();
  326.            if (!ShowIcon) RemoveAppMenuItem(OTWAppMenuItem);
  327.    case  8:RemoveAppMenuItem(QTMAppMenuItem);
  328.    case  7:DeleteCxObjAll(MyBroker);                /* Remove CX Objects */
  329.    case  6:while (msg=GetMsg(MyBrokerPort)) ReplyMsg(msg);
  330.            DeleteMsgPort(MyBrokerPort);
  331.    case  5:RemPort(MyMP);                           /* Remove message port */
  332.            while (msg=GetMsg(MyMP)) ReplyMsg(msg);  /* Reply all messages */
  333.            DeleteMsgPort(MyMP);
  334.    case  4:CloseLibrary(CxBase);
  335.    case  3:CloseLibrary(IconBase);
  336.    case  2:CloseLibrary(WorkbenchBase);
  337.    case  1:break;
  338.   }
  339.  
  340.  /* Did we have a console window? */
  341.  if (OldConsoleTask)
  342.   {
  343.    struct Process *pr=FindTask(NULL);
  344.  
  345.    pr->pr_ConsoleTask=OldConsoleTask; /* Yes, restore old value */
  346.   }
  347.  
  348.  exit(RETURN_OK);    /* all o.k. */
  349. }
  350.